package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.eventbus_event.BREvent;

import de.greenrobot.event.EventBus;

/**
 * Created by xiaoke.ni on 2017/10/25.
 */

public class BRCFDRemarkFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private EditText mGoodsDescrip;
    private Button mSureBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_br_cfd_remark, null);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void initEvent() {
        mSureBtn.setOnClickListener(this);

        mGoodsDescrip.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mSureBtn.setBackground(getResources().getDrawable(R.drawable.common_btn1_press_shape));
                    mSureBtn.setEnabled(true);
                } else {
                    mSureBtn.setBackground(getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
                    mSureBtn.setEnabled(false);
                }
            }
        });
    }

    private void initView() {
        mGoodsDescrip = (EditText) mView.findViewById(R.id.goods_descrip);
        mSureBtn = (Button) mView.findViewById(R.id.sure_btn);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.sure_btn){
            BREvent event = new BREvent(BREvent.REMARK);
            event.setRemark(mGoodsDescrip.getText().toString().trim());
            EventBus.getDefault().post(event);
            popFragment();
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BRCFDRemarkFragment) {
            mActivity.setTitle(getString(R.string.user_br_remake));
        }
    }

    @Override
    public void requestDatas() {
        initView();
        initEvent();

        mGoodsDescrip.setText(getArguments().getString("remark"));
    }

}
